package com.schoolcloud.files.controller;

import cn.hutool.core.date.DateUtil;
import com.schoolcloud.common.model.R;
import com.schoolcloud.files.config.FileConfig;
import com.schoolcloud.files.dto.ChunkMergeDTO;
import com.schoolcloud.files.dto.FileInfoDTO;
import com.schoolcloud.files.entity.FileUploadScene;
import com.schoolcloud.files.service.FileService;
import com.schoolcloud.files.service.FileUploadSceneService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/file/upload")
@RequiredArgsConstructor
public class FileUploadController {

    private final FileService fileService;

    @PostMapping("/common")
    public R<FileInfoDTO> commonUpload(MultipartFile file,String sceneCode) {
        FileInfoDTO dto = fileService.commonUpload(file, sceneCode);
        return R.ok(dto);
    }

    @PostMapping("/avatar")
    public R<Object> uploadUserAvatar(MultipartFile file) {
        try {
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.AVATAR_BUCKET, "", "avatar");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/bard")
    public R<Object> uploadSocialBardFile(MultipartFile file) {
        try {
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, "bard", "bard");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/post")
    public R<Object> uploadSocialPostFile(MultipartFile file) {
        try {
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, "post", "post");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/post/reply")
    public R<Object> uploadSocialPostReplyFile(MultipartFile file) {

        try {
            String baseUrl = "post/reply";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, baseUrl,"post-reply");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/social/chat")
    public R<Object> uploadSocialChatFile(MultipartFile file) {
        try {
            String baseUrl = "chat";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, baseUrl,"chat");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/event/resource")
    public R<Object> uploadSocialEventResourceFile(MultipartFile file) {
        try {
            String baseUrl = "event/resource";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, baseUrl,"event-resource");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/event/cover")
    public R<Object> uploadSocialEventCoverFile(MultipartFile file) {
        try {
            String baseUrl = "event/cover";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, baseUrl,"event-cover");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/event/mien")
    public R<Object> uploadSocialEventMienFile(MultipartFile file) {
        try {
            String baseUrl = "event/mien";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SOCIAL_PUBLIC_BUCKET, baseUrl,"event-mien");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/serve/lost-found")
    public R<Object> uploadLostAndFoundFile(MultipartFile file) {
        try {
            String baseUrl = "lost-found";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SERVE_PUBLIC_BUCKET, baseUrl,"lost-found");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }

    @PostMapping("/serve/re-market")
    public R<Object> uploadRemarketFile(MultipartFile file) {
        try {
            String baseUrl = "re-market";
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SERVE_PUBLIC_BUCKET, baseUrl,"re-market");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        } catch (Exception e) {
            return R.err("上传失败!");
        }
    }
    @PostMapping("/share/video/cover")
    public R<?> uploadShareVideoCoverFile(@RequestParam MultipartFile file) {
        String baseUrl = "video/cover";
        try {
            FileInfoDTO dto = fileService.uploadFile(file, FileConfig.SHARE_PUBLIC_BUCKET, baseUrl,"video-cover");
            return R.ok("上传成功！",FileConfig.PROFILE + "/" + dto.getStoragePath());
        }catch (Exception e){
            return R.err("上传失败!");
        }
    }
    @PostMapping("/chunk")
    public R<?> uploadShareVideoChunkFile( MultipartFile file,  String md5,int chunk, String sceneCode) {
        boolean b = fileService.uplaodChunk(sceneCode, file, md5, chunk);
        return b ? R.ok(b) : R.err("上传失败!");
    }
    @PostMapping("/chunk/merge")
    public R<?> mergeChunk(@RequestBody ChunkMergeDTO dto) {
        return fileService.mergeChunk(dto);
    }
}
